this.onmessage = function (event) {
    let {data, type} = event.data;
    let startTime = Math.min(...data.map(item => new Date(item.startTime).getTime()));
    let endTime = Math.max(...data.map(item => new Date(item.endTime).getTime()));
    postMessage(type === 'day' ? handleDay({
        ...event.data,
        startTime,
        endTime
    }) : handleMonth({startTime, endTime, type}));
}

function handleDay({startTime, endTime, startHour, endHour, type}) {
    let dyColumns = [];
    let dyColumn = [];
    while (startTime <= endTime) {
        let date = dateFormat(startTime, type)
        let obj = {
            key: date,
            title: date,
            children: []
        }
        for (let i = startHour; i <= endHour; i++) {
            let key = `${date}-${i}`;
            obj.children.push({
                key,
                title: i + '',
                width: 60
            });
            dyColumn.push(key);
        }
        startTime += 24 * 60 * 60 * 1000;
        dyColumns.push(obj);
    }
    return {
        dyColumns,
        dyColumn
    }
}

function handleMonth({startTime, endTime, type}) {
    let dyColumns = [];
    let dyColumn = [];
    let dateObj = {};
    for (let i = startTime; i <= endTime; i += 24 * 60 * 60 * 1000) {
        let date = dateFormat(i, type);
        let day = new Date(i).getDate().toString();
        let key = `${date}-${day}`;
        dyColumn.push(key);
        if (dateObj[date]) {
            dateObj[date].children.push({
                key,
                title: day,
                width: 60
            });
        } else {
            dateObj[date] = {
                key: date,
                title: date,
                children: [{
                    key,
                    title: day,
                    width: 60
                }]
            };
        }
    }
    dyColumns = Object.values(dateObj);
    return {
        dyColumns,
        dyColumn
    }
}

function dateFormat(date, type) {
    date = new Date(date);
    return type === 'month' ? `${date.getFullYear()}/${(date.getMonth() + 1).toString().padStart(2, '0')}`
            : `${date.getFullYear()}/${(date.getMonth() + 1).toString().padStart(2, '0')}/${date.getDate().toString().padStart(2, '0')}`
}

